<?php
// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}

$placeholder_folder = wp_upload_dir();
DEFINE("PLACEHOLDER", $placeholder_folder['baseurl'] . '/placeholder.png');

$includes = array(
	'inc/function-enqueue.php', 		// Load scripts and styles;
	'inc/function-general.php', 		// Load Theme founctions;
	'inc/function-widgets.php', 		// Load Widgets;
	'inc/wp_bootstrap_navwalker.php',	// Register Custom Navigation Walker
	'inc/function-seo.php',				// Website SEO
	'inc/class-post-type.php',				// Website Post Type
	'inc/class-taxonomy.php',				// Website Post Type Tax
	'inc/aq_resizer.php'				//   * USAGE:    $aq_image = aq_resize( $img_url, $image_size['width'], $image_size['height'], true );
);
foreach ($includes as $i) {
    locate_template($i, true);
}

/* Custom Post Type Start */
new Custom_Post_Type_Post($post_type = 'product', $plural = '产品', $single = '产品', $description = '', $has_archive = true);
new Custom_Taxonomy($taxonomy = 'product-category', $plural = '产品分类', $single = '产品分类', $post_types = array('product'));

/* Custom Post Type End */

$mytypes = array(//根据需要添加你的自定义文章类型
	'product' => 'product'
);
add_filter('post_type_link', 'my_custom_post_type_link', 1, 3);
function my_custom_post_type_link( $link, $post = 0 ){
	global $mytypes;
	if ( in_array( $post->post_type,array_keys($mytypes) ) ){
		return home_url( $mytypes[$post->post_type].'/' . $post->ID );
	} else {
		return $link;
	}
}
add_action( 'init', 'my_custom_post_type_rewrites_init' );
function my_custom_post_type_rewrites_init(){
	global $mytypes;
	foreach( $mytypes as $k => $v ) {
		add_rewrite_rule(
			$v.'/([0-9]+)?$',
			'index.php?post_type='.$k.'&p=$matches[1]',
			'top'
		);
		add_rewrite_rule(
			$v.'/([0-9]+)?/comment-page-([0-9]{1,})$',
			'index.php?post_type='.$k.'&p=$matches[1]&cpage=$matches[2]',
			'top'
		);
	}
}

//protect dashboard login
add_action('login_enqueue_scripts','login_protection');  
function login_protection(){  
    if($_GET['site'] != 'zfshijia')header('Location: '.home_url());  
}


// Add Product Shortcode
function product_shortcode( $atts , $content = null ) {
	// Attributes
	extract( shortcode_atts(
		array(
			'posts_per_page' => '',
			'tax_term' => '',
			'operator_tax_term' => ''
		), $atts )
	);
	$output = '<div class="product-more-list"><div class="row">';
	$the_query = new WP_Query( 
		array ( 
			'posts_per_page' => '-1',  
			'post_type' => 'product', 
			'orderby' => 'date',  
			'order' => 'ASC',
			'tax_query' => array(
				'relation' => 'AND',
				array(
					'taxonomy' => 'product-category',
					'field'    => 'term_id',
					'terms' => $tax_term
				),
				array(
					'taxonomy' => 'product-category',
					'field'    => 'term_id',
					'terms' => $operator_tax_term,
					'operator' => 'NOT IN'
				)
			) 
		) 
	);

	while ( $the_query->have_posts() ):
		$the_query->the_post();
		$thumb_id = get_post_thumbnail_id($post -> ID);
		$image_url = wp_get_attachment_url($thumb_id);
		$image_vals = aq_resize( $image_url, 370, 260, true );
		
		if(!$image_vals){
			$image_vals = aq_resize( PLACEHOLDER, 370, 260, true );
		}

		if($tax_term == '8'){
			if(get_field('background_color')){
				$bg_color = '-'.get_field('background_color');
			} else {
				$bg_color = '';	
			}

			$output .= '<div class="col-xs-12 col-mo-6 col-sm-4">';			
			$output .= '<article class="bg'.$bg_color.'">';
			$output .= '<div class="img-wrapper">';
			$output .= '<img src="'.$image_vals.'" /></div>';
			$output .= '<div class="type-info '.get_field('font_color').'">';
			$output .= '<h4>'. get_the_title() .'</h4>';
			$output .= '<div class="product-content">'.get_the_content().'</div>';
			$output .= '</div></article></div>';
		} else if($tax_term == '9'){
			$output .= '<div class="col-xs-12 col-mo-6">';			
			$output .= '<article class="grid-article">';
			$output .= '<div class="img-wrapper">';
			$output .= '<img src="'.$image_vals.'" /></div>';
			$output .= '<div class="type-info '.get_field('font_color').'">';
			$output .= '<h4>'. get_the_title() .'</h4>';
			$output .= '<div class="product-content">'.get_the_content().'</div>';
			$output .= '</div></article></div>';
		}
	endwhile;
	wp_reset_postdata();
	$output .= '</div></div>';
	return $output;
}
add_shortcode( 'at_product', 'product_shortcode' );